package com.qzy.demo.service.impl;

import com.qzy.demo.pojo.GradePojo;
import com.qzy.demo.pojo.StudentPojo;
import com.qzy.demo.service.GradeService;
import com.qzy.demo.service.SolrService;
import com.qzy.demo.service.StudentService;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class SolrServiceImpl implements SolrService {

    @Autowired
    private StudentService studentService;
    @Autowired
    private GradeService gradeService;
    @Autowired
    private SolrClient solrClient;
    @Override
    public boolean importAllSolrIndex() throws IOException, SolrServerException {
        List<StudentPojo> list = new ArrayList<StudentPojo>();
        List<GradePojo> listGrade = new ArrayList<GradePojo>();
        SolrInputDocument doc = null;
        //先清除所有索引
        solrClient.deleteByQuery("*:*");
        solrClient.commit();
        //导入所有索引
        list = studentService.getAllStudents();
        for (StudentPojo stu:list) {
            stu.setUuid("stu_uuid_"+stu.getSid());
        }
        solrClient.addBeans(list);
        solrClient.commit();

        listGrade = gradeService.getAllGrades();
        for (GradePojo grade:listGrade ) {
            grade.setUuid("grade_uuid_"+grade.getGid());
        }
        solrClient.addBeans(listGrade);
        solrClient.commit();
        return true;
    }
}
